import React, { useState, useEffect, useCallback } from "react";
import { Select } from "antd";
import request from "@/utils/request";
import { stringify } from "qs";
import Style from "./index.module.less";
// 查询平台店铺列表
export async function queryShopList(params) {
	const data = { current: 1, size: 100, ...params };
	return request(`/blade-goods/newSystem/ecStore/list/page`, {
		method: "POST",
		data: data
	});
}

const SelectShop = props => {
	const { value, onChange, onChangeAll, className, valueKey, ...restProps } = props;

	const [shopList, setShopList] = useState([]);
	const [options, setOptions] = useState([]);
	const vk = valueKey || "id";
	useEffect(() => {
		// {
		//     "id": 37110,
		//     "uuid": 3003125,
		//     "storeId": "594869",
		//     "storeName": "杜尚测试-聚水潭",
		//     "isMergeOrder": 0,
		//     "typeName": "聚水潭",
		//     "serviceTime": 1970898480,
		//     "authExpireTime": 1770898480,
		//     "type": 20,
		//     "customBackFlowTime": 47,
		//     "inAdvanceBackFlowTime": 1
		// }
		queryShopList().then(({ success, data }) => {
			if (success && data && Array.isArray(data.records)) {
				setShopList(data.records);

				setOptions(
					data.records.map(e => {
						return {
							value: e[vk],
							label: e.systemStoreName || e.storeName
						};
					})
				);
			} else {
				setShopList([]);
				setOptions([]);
			}
		});
	}, []);

	const handleChange = useCallback(
		value => {
			onChange && onChange(value);
			const item = shopList.find(e => e[vk] == value);
			onChangeAll && onChangeAll(value, item);
		},
		[shopList, onChange, onChangeAll]
	);

	const selectClassName = Style.shop_select + " height32 " + className + " before-text-shop-select ";
	return (
		<Select
			className={selectClassName}
			value={value}
			style={{ width: 120 }}
			onChange={handleChange}
			options={options}
			{...restProps}
		/>
	);
};

export default React.memo(SelectShop);
